/*
 * StoreItemRelEditor.java
 *
 * Created on August 3, 2006, 1:49 AM
 */

package com.floreantpos.ui.model;

import java.util.List;

import com.floreantpos.model.MenuItem;
import com.floreantpos.model.StoreItemRel;
import com.floreantpos.model.dao.MenuItemDAO;
import com.floreantpos.model.dao.StoreDAO;
import com.floreantpos.model.dao.StoreItemRelDAO;
import com.floreantpos.swing.ComboBoxModel;
import com.floreantpos.swing.MessageDialog;
import com.floreantpos.ui.BeanEditor;

/**
 *
 * @author  David
 */
public class StoreItemRelForm extends BeanEditor {

    /** Creates new form StoreEditor */
    public StoreItemRelForm() {
    	this(new StoreItemRel());
    }

    public StoreItemRelForm(StoreItemRel storeItemRel) {
    	initComponents();
    	
    	setBean(storeItemRel);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        fNumber = new javax.swing.JFormattedTextField();
        fItem = new javax.swing.JComboBox();
        fStore = new javax.swing.JComboBox();

        jLabel1.setText(com.floreantpos.POSConstants.MENU_ITEMS + ":");

        jLabel2.setText(com.floreantpos.POSConstants.STORE_NAME + ":");

        jLabel3.setText(com.floreantpos.POSConstants.NUMBER + ":");

        fNumber.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        // set combox data
		MenuItemDAO menuItemDAO = new MenuItemDAO();
		List menuItems = menuItemDAO.findAll();
		fItem.setModel(new ComboBoxModel(menuItems));

        // set combox data
		StoreDAO storeDAO = new StoreDAO();
		List stores = storeDAO.findAllGroupByName();
		fStore.setModel(new ComboBoxModel(stores));

		fNumber.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
	        this.setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
	            .add(layout.createSequentialGroup()
	                .addContainerGap()
	                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
	                    .add(jLabel3)
	                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
	                        .add(jLabel1)
	                        .add(jLabel2)))
	                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
	                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
	                    .add(fNumber)
	                    .add(fStore)
	                    .add(fItem, 0, 189, Short.MAX_VALUE))
	                .addContainerGap(23, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
	            .add(layout.createSequentialGroup()
	                .addContainerGap()
	                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
	                    .add(jLabel1)
	                    .add(fItem, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
	                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
	                    .add(jLabel2)
	                    .add(fStore, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
	                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
	                    .add(jLabel3)
	                    .add(fNumber, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
	                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	        );
	    }// </editor-fold>
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox fItem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JFormattedTextField fNumber;
    private javax.swing.JComboBox fStore;
    // End of variables declaration//GEN-END:variables
	@Override
	public boolean save() {
		
		try {
			if(!updateModel()) return false;
			
			StoreItemRel storeItemRel = (StoreItemRel) getBean();
			StoreItemRelDAO dao = new StoreItemRelDAO();
			dao.saveOrUpdate(storeItemRel);
		} catch (Exception e) {
			e.printStackTrace();
			MessageDialog.showError(e);
			return false;
		}
		
		return true;
	}

	@Override
	public void dispose() {
	}

	@Override
	protected void updateView() {
		StoreItemRel storeItemRel = (StoreItemRel) getBean();
		
		if(storeItemRel.getItem() != null)
			fItem.setSelectedItem(storeItemRel.getItem());
		
		fStore.setSelectedItem(storeItemRel.getStoreName());
		fNumber.setValue(storeItemRel.getNumber());
	}

	@Override
	protected boolean updateModel() {
		StoreItemRel storeItemRel = (StoreItemRel) getBean();
		
		Object item = fItem.getSelectedItem();
    	if(item == null) {
    		MessageDialog.showError(com.floreantpos.POSConstants.ITEM_REQUIRED);
    		return false;
    	}
    	
    	Object name = fStore.getSelectedItem();
    	if(name == null) {
    		MessageDialog.showError(com.floreantpos.POSConstants.STORE_REQUIRED);
    		return false;
    	}
    	
    	storeItemRel.setItem((MenuItem)item);
    	storeItemRel.setStoreName((String)fStore.getSelectedItem());
    	storeItemRel.setNumber(Integer.valueOf(fNumber.getText()));
		return true;
	}
    
	public String getDisplayText() {
		StoreItemRel storeItemRel = (StoreItemRel) getBean();
    	if(storeItemRel.getId() == null) {
    		return com.floreantpos.POSConstants.STORE_NEW;
    	}
    	return com.floreantpos.POSConstants.STORE_EDIT;
    }
	
}
